package com.you.raft.rpc.impl;

import com.you.raft.rpc.RpcRaftClient;
import com.you.raft.common.entity.Request;
import com.you.raft.common.entity.Response;
import com.you.rpc.RpcClient;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DefaultRpcClient implements RpcRaftClient {
    private final RpcClient rpcClient = new RpcClient();

    @Override
    public void init() throws Exception {
        rpcClient.start();
    }

    @Override
    public void destory() throws Exception {
        rpcClient.destroy();
    }

    @Override
    public <T> T send(Request request) {
        return send(request, 3);
    }

    @Override
    public <T> T send(Request request, int timeout) {
        String address = request.getAddr();
        try {
            Response object = (Response) rpcClient.invokeSync(address, request, timeout);
            if (object != null) {
                return (T) object.getResult();
            }
        } catch (Exception e) {
//            throw new RpcException();
//            e.printStackTrace();
        }
        return null;
    }
}
